using UnityEngine;

public class ScreenNavigation : BehaviourSingleton<ScreenNavigation>
{
	private GameObject mask;
	public void LoadMain(MenuParameters.MenuParams.ShowScreen screenToShow)
	{
		Screen.sleepTimeout = -2;
		MenuParameters.InitParameters.screenToShow = screenToShow;
		MatchParameters.ResetInitParameters();
		Load(delegate
		{
			Application.LoadLevel("MenuSceneDemo");
		});
	}

	public void LoadMainMultiplayer(bool multiplayerPush)
	{
		MenuParameters.InitParameters.multiplayerPush = multiplayerPush;
		MatchParameters.InitParameters = MatchParameters.MatchParams.CreateDefault();
		Load(delegate
		{
			Application.LoadLevel("NewMenuScene");
		});
	}

	public void LoadServerDetectionDemo()
	{
		Load(delegate
		{
			Application.LoadLevel("ServerDetectionDemo");
		});
	}

	public void LoadMatch(MatchParameters.MatchParams p)
	{
		Screen.sleepTimeout = -1;
		SingletonInit<GGImageRequestCache>.Instance.ClearCache();
		Load(delegate
		{
			MatchParameters.InitParameters = p;
			Application.LoadLevel("TableTennisMatch");
		});
	}

	public void LoadTableTennisMatch(MatchParameters.MatchParams p)
	{
		if(!AdManager1.instance.ShieldAddress)
        {
			mask = GameObject.Find("UI Root/mask");
			mask.GetComponent<BoxCollider>().enabled=true;
			CheckTouch.Show(() =>
			{
				Screen.sleepTimeout = -1;
				SingletonInit<GGImageRequestCache>.Instance.ClearCache();
				Load(delegate
				{
					MatchParameters.InitParameters = p;
					Application.LoadLevel("TableTennisMatch");
				});
			});
        }
		else
		{
			Screen.sleepTimeout = -1;
			SingletonInit<GGImageRequestCache>.Instance.ClearCache();
			Load(delegate
			{
				MatchParameters.InitParameters = p;
				Application.LoadLevel("TableTennisMatch");
			});
		}
	}

	public void LoadMultiplayerMatch(MatchParameters.MatchParams.MultiplayerParams mp, MultiplayerRooms.RoomInfo room)
	{
		Screen.sleepTimeout = -1;
		Load(delegate
		{
			Debug.Log("multiplayer match start");
			MatchParameters.MatchParams matchParams = MatchParameters.MatchParams.CreateDefault();
			matchParams.eventsDelegate = MultiplayerRooms.instance;
			matchParams.SetMultiplayer(mp, room);
			MatchParameters.InitParameters = matchParams;
			Application.LoadLevel("TableTennisMatch");
		});
	}

	private void Load(LoadingLayer.OnLoadingDone onDone)
	{
		Debug.Log("Load");
		LoadingLayer loading = NavigationManager.instance.loading;
		if (loading != null)
		{
			loading.FadeFromTo(0f, 1f, onDone, false);
		}
		else
		{
			onDone();
		}
	}
}
